package com.renjibo.sharkerwave.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.ViewGroup;

import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.ui.fragment.CourseFragment;
import com.renjibo.sharkerwave.ui.fragment.MyFragment;
import com.renjibo.sharkerwave.ui.fragment.StudyFragment;
import com.renjibo.sharkerwave.ui.widget.IconTextView;
import com.sharker.lazyviewpager.LazyFragmentPagerAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageBottomTabLayout;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;

/**
 * 首页
 */
@ContentView(R.layout.activity_home)
public class HomeActivity extends BaseActivity  {

    private ViewPager viewPager;
    private LazyFragmentPagerAdapter lazyFragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

//        ActionBar actionBar = getSupportActionBar();
//        actionBar.hide();
//        addHeader();
        initView();
    }

    void initView() {
        PageBottomTabLayout tab = (PageBottomTabLayout) findViewById(R.id.tab);

        //注意这里调用了custom()方法
        final NavigationController navigationController = tab.custom()
                .addItem(newItem(R.mipmap.common_course_unselected, R.mipmap.common_course_selected, "学习"))
                .addItem(newItem(R.mipmap.common_study_unselected, R.mipmap.common_study_selected, "课程"))
                .addItem(newItem(R.mipmap.common_mine_unselected, R.mipmap.common_mine_selected, "我的"))
                .build();

        viewPager = (ViewPager) findViewById(R.id.lazyViewPager);
        lazyFragmentPagerAdapter = new LazyFragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            protected Fragment getItem(ViewGroup container, int position) {
                if (position == 0) {
                    return StudyFragment.newInstance("", "");
                } else if (position == 1) {
                    return CourseFragment.newInstance("", "");
                } else if (position == 2) {
                    return MyFragment.newInstance("", "");
                } else {
                    return StudyFragment.newInstance("", "");
                }
            }

            @Override
            public int getCount() {
                return navigationController.getItemCount();
            }
        };
        viewPager.setAdapter(lazyFragmentPagerAdapter);
        navigationController.setupWithViewPager(viewPager);
    }
    //创建一个Item
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text) {
        IconTextView normalItemView = new IconTextView(this);
        normalItemView.initialize(drawable, checkedDrawable, text);
        normalItemView.setTextDefaultColor(Color.GRAY);
        normalItemView.setTextCheckedColor(0xFF009688);
        return normalItemView;
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //TODO 处理传入参数
    }

}
